package com.bosssoft.login.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bosssoft.login.dao.UserMapper;
import com.bosssoft.login.entity.dto.UserDTO;
import com.bosssoft.login.entity.po.UserPO;
import com.bosssoft.login.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDTO getUserByUserName(UserDTO userDTO) {
        UpdateWrapper<UserPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_name", userDTO.getUsername());

        UserPO userPO = userMapper.selectOne(updateWrapper);
        UserDTO userDTO01 = new UserDTO();
        userDTO01.setUid(userPO.getUid());
        userDTO01.setPassword(userPO.getPassword());
        userDTO01.setPhone(userPO.getPhone());
        userDTO01.setUsername(userPO.getUsername());

        return userDTO01;
    }

    /*
    * 检验注册用户是否存在
    * 用户名不能一样
    */
    @Override
    public boolean getUser(UserDTO userDTO) {

        UpdateWrapper<UserPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_name", userDTO.getUsername());

        UserPO userPO = userMapper.selectOne(updateWrapper);
        // 用户存在
        if(userPO != null){
            return true;
        }
        // 用户不存在
        else {
            return false;
//            UserDTO userDTO01 = new UserDTO();
//            userDTO01.setUid(userPO.getUid());
//            userDTO01.setPassword(userPO.getPassword());
//            userDTO01.setPhone(userPO.getPhone());
//            userDTO01.setUsername(userPO.getUsername());
        }
    }

    @Override
    public Integer insertUser(UserDTO userDTO) {
        UserPO userPO = new UserPO();
        userPO.setPassword(userDTO.getPassword());
        userPO.setPhone(userDTO.getPhone());
        userPO.setUsername(userDTO.getUsername());
        Integer result = userMapper.insert(userPO);
        return result;
    }
}